<?php
/**
 * @copyright 
 * @license https://opensource.org/licenses/Apache-2.0
 * @link https://www.gougucms.com
 */

declare (strict_types = 1);

namespace app\admin\controller;

use app\admin\BaseController;
use think\facade\Db;
use think\facade\View;

class Index extends BaseController
{

    public function map()
    {
        if (request()->isAjax()) {
            $points = Db::name('device')->alias('d')
                        ->join('user_one u','d.user_one=u.id')
                        ->field('d.*,u.user_name,u.mobile,u.lng,u.lat,u.address')
                        ->where(['d.delete_time'=>0])
                        ->select()
                        ->toArray();
            //设备总数
            $count = count($points);
            return success($count,$points);
        } else {
            return view();
        }
    }

    public function index()
    {
		$admin = get_login_admin();
		if (get_cache('menu' . $admin['id'])) {
			$list = get_cache('menu' . $admin['id']);
		} else {
			$adminGroup = Db::name('AdminGroupAccess')->where(['uid' => get_login_admin('id')])->column('group_id');
			$adminMenu = Db::name('AdminGroup')->where('id', 'in', $adminGroup)->column('rules');
			$adminMenus = [];
			foreach ($adminMenu as $k => $v) {
				$v = explode(',', $v);
				$adminMenus = array_merge($adminMenus, $v);
			}
			$menu = Db::name('AdminRule')->where(['menu' => 1,'status'=>1])->where('id', 'in', $adminMenus)->order('sort asc')->select()->toArray();
			$list = list_to_tree($menu);
			\think\facade\Cache::tag('adminMenu')->set('menu' . $admin['id'], $list);
		}
        $web = get_system_config("web");
        if($web['logo'])
        {
            $web['logo'] = cdnurl($web['logo']);
        }
		$theme = Db::name('Admin')->where('id',$admin['id'])->value('theme');
		View::assign('web',$web);
		View::assign('theme',$theme);
        View::assign('menu', $list);
        return View();
    }

    public function main()
    {
        $adminCount = Db::name('Admin')->where('status', '1')->count();
        $userCount = Db::name('User')->where('status', '1')->count();
        $fileCount = Db::name('File')->count();
        $install = false;
        if (file_exists(CMS_ROOT . 'app/install')) {
            $install = true;
        }
        //线索数量
        $conf['xiansuo'] = Db::name('news')->order('id desc')->count();// Db::name('news_total')->where('id',1)->value('total');//;Db::name('news')->where(['delete_time'=>0])->count();
        //会员数量
        $conf['vip_num'] = Db::name('user')->where([['is_vip_one|is_svip_one|is_vip_two|is_svip_two','=',1]])->count();
        $conf['user_num'] = Db::name('user')->where(['user_type'=>1])->count();
        $conf['gongsi_num'] = Db::name('user')->where(['user_type'=>2])->count();
        $conf['user_total'] = Db::name('user')->count();
        View::assign('conf', $conf);
        View::assign('adminCount', $adminCount);
        View::assign('userCount', $userCount);
        View::assign('fileCount', $fileCount);
        View::assign('install', $install);
        View::assign('TP_VERSION',\think\facade\App::version());
        return View();
    }
	
	//设置theme
	public function set_theme()
    {
        if (request()->isAjax()) {
            $param = get_params();
			$admin = get_login_admin();
			Db::name('Admin')->where('id',$admin['id'])->update(['theme'=>$param['theme']]);
            return to_assign();
        }
		else{
			return to_assign(1,'操作错误');
		}
    }
}
